#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

#define  FIFO_TCP   "/tmp/nsgbot_tcpQ"
#define  FIFO_IRC   "/tmp/nsgbot_ircQ"
#define  FIFO_HTTP  "/tmp/nsgbot_httpQ"

#include "../nsgbot_capture/pseudo_pkt.h"

#define TAG_PING 1000001
#define TAG_PONG 1000002
#define TAG_USER 1000003
#define TAG_NICK 1000004
#define TAG_JOIN 1000005
#define TAG_USERHOST 1000006
#define TAG_MODE 1000007
#define TAG_PRIVMSG 1000008
#define TAG_NOTICE 1000010

#define TAG_GET 3000001
#define TAG_POST 3000002

int main( void)
{
  int   fd;
  int irc_fd;
  int http_fd;
  pseudo_pkt *ppkt;

  // Open TCP pipe
  if ( -1 == ( fd = open( FIFO_TCP, O_RDWR))) 
	{
	  perror( "open() name pipe : TCP");
	  exit(1);
	}

  // Open IRC pipe
  if ( -1 == ( irc_fd = open( FIFO_IRC, O_RDWR)))
	{
	  perror( "open() name pipe : IRC");
	  exit(1);
	}

  // Open http Pipe
  if ( -1 == (http_fd = open( FIFO_HTTP, O_RDWR)))
	{
	  perror( "open() named pipe : http");
	  exit(1);
	}
  
  // temp memory
  if ( (ppkt = (pseudo_pkt*)calloc(1, sizeof(pseudo_pkt))) == NULL) {
	fprintf(stderr, "Calloc error in nsgbot_tcp\n");
	exit;
  }

  // loop
  while( 1 )
	{
	  read( fd, (void*)ppkt, sizeof(pseudo_pkt));

	  // Payload Check
	  if (strncmp(ppkt->payload, "GET ", 4) == 0) {
		ppkt->tag = TAG_GET;
		write(http_fd, (void*)ppkt, sizeof(pseudo_pkt));
		//print_pkt(ppkt, 1);
		continue;
	  }
	  else if (strncmp(ppkt->payload, "POST ", 5) == 0) {
		//print_pkt(ppkt, 1);
		ppkt->tag = TAG_POST;
		//write(http_fd, (void*)ppkt, sizeof(pseudo_pkt));
		continue;
	  }

	  // IRC signature
	  else if ( strncmp(ppkt->payload, "PING :", 6) == 0) {
		ppkt->tag = TAG_PING;
		write(irc_fd, (void*)ppkt, sizeof(pseudo_pkt));
		continue;
	  }
	  else if ( strncmp(ppkt->payload, "PONG :", 6) == 0) {
		ppkt->tag = TAG_PONG;
		write(irc_fd, (void*)ppkt, sizeof(pseudo_pkt));
		continue;
	  }
	  else if ( strncmp(ppkt->payload, "USER ", 5) == 0 ) {
		ppkt->tag = TAG_USER;
		write(irc_fd, (void*)ppkt, sizeof(pseudo_pkt));
		continue;
	  }
	  else if ( strncmp(ppkt->payload, "NICK ", 5) == 0) {
		ppkt->tag = TAG_NICK;
		write(irc_fd, (void*)ppkt, sizeof(pseudo_pkt));
		continue;
	  }
	  else if ( strncmp(ppkt->payload, "USERHOST ", 9) == 0) {
		ppkt->tag = TAG_USERHOST;
		write(irc_fd, (void*)ppkt, sizeof(pseudo_pkt));
		continue;
	  }
	  else if ( strncmp(ppkt->payload, "JOIN #", 6) == 0) {
		ppkt->tag = TAG_JOIN;
		write(irc_fd, (void*)ppkt, sizeof(pseudo_pkt));
		continue;
	  }
	  else if ( strncmp(ppkt->payload, "MODE ", 5) == 0) {
		ppkt->tag = TAG_MODE;
		write(irc_fd, (void*)ppkt, sizeof(pseudo_pkt));
		continue;
	  }
	  else if ( strncmp(ppkt->payload, "PRIVMSG ", 8) == 0) {
		ppkt->tag = TAG_PRIVMSG;
		write(irc_fd, (void*)ppkt, sizeof(pseudo_pkt));
		continue;
	  }
	  else if ( strncmp(ppkt->payload, "NOTICE ", 7) == 0) {
		ppkt->tag = TAG_NOTICE;
		write(irc_fd, (void*)ppkt, sizeof(pseudo_pkt));
		continue;
	  }
	  else if ( strncmp(ppkt->payload, ".u PRIVMSG ", 11) == 0) {
		ppkt->tag = TAG_PRIVMSG;
		write(irc_fd, (void*)ppkt, sizeof(pseudo_pkt));
		continue;
	  }
	  
	}
  close( fd);
}
